package firehwak.thoth.client;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

import firehwak.thoth.client.data.Data;
import firehwak.thoth.client.database.LocalDatabase;
import firehwak.thoth.client.location.LocationHelper;
import firehwak.thoth.client.location.MarkerLayer;
import firehwak.thoth.client.service.ThothService;

public class Prototype2Activity extends MapActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		initializeDatabase();
		initializeLocation();
		initializeUI();

		startService(new Intent(this, ThothService.class));
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	// Location
	private LocationHelper locationHelper;

	private void initializeLocation() {
		locationHelper = new LocationHelper((LocationManager) getSystemService(Context.LOCATION_SERVICE),
			new LocationHelper.CustomLocationListener() {
				public void onLocationUpdate() {
					updateLocation();
				}
			});
		locationHelper.Start();
	}

	public void updateLocation() {
		Location location = locationHelper.getLocation();
		if (location != null) {
			getLocationOverlay().onLocationChanged(location);
			mapView.getController().animateTo(getGeoPoint(location));
			mapView.postInvalidate();
		}
		locationHelper.Stop();

		updateMarkers();
	}

	// UI
	private MapView mapView;
	private MyLocationOverlay myLocationOverlay;

	private void initializeUI() {
		mapView = (MapView) findViewById(R.id.mapView);
		mapView.getController().setZoom(19);
	}

	private void updateMarkers() {
		mapView.getOverlays().clear();
		mapView.getOverlays().add(getLocationOverlay());

		Location prevLocation = null;
		List<Data> list = database.getDataList();
		for (Data data : list) {
			Location location = new Location(LocationManager.GPS_PROVIDER);
			location.setLatitude(data.latitude);
			location.setLongitude(data.longitude);

			if (prevLocation != null) {
				if (prevLocation.distanceTo(location) < 40) {
					continue;
				}
			}

			OverlayItem overlayItem = new OverlayItem(getGeoPoint(location), "SignalStrength",
				String.valueOf((int) data.strength) + " " + String.valueOf(data.timestamp));

			MarkerLayer markerLayer;
			Drawable marker;

			if (data.strength < 12) {
				marker = getResources().getDrawable(R.drawable.red);
				marker.setAlpha(16 - (int) data.strength);
				markerLayer = new MarkerLayer(marker, this);
				markerLayer.addOverlayItem(overlayItem);

			} else {
				marker = getResources().getDrawable(R.drawable.green);
				marker.setAlpha(2 + (int) data.strength);
				markerLayer = new MarkerLayer(marker, this);
				markerLayer.addOverlayItem(overlayItem);
			}
			mapView.getOverlays().add(markerLayer);

			prevLocation = location;
		}

		mapView.postInvalidate();

		Toast.makeText(this, "updateMarkers", Toast.LENGTH_SHORT).show();
	}

	// Database
	LocalDatabase database;

	private void initializeDatabase() {
		database = new LocalDatabase(this);
		database.open();
	}

	private GeoPoint getGeoPoint(Location location) {
		if (location == null) {
			return null;
		}

		Double lat = location.getLatitude() * 1E6;
		Double lng = location.getLongitude() * 1E6;

		return new GeoPoint(lat.intValue(), lng.intValue());
	}

	private MyLocationOverlay getLocationOverlay() {
		if (myLocationOverlay == null) {
			myLocationOverlay = new MyLocationOverlay(this, mapView);
			myLocationOverlay.enableMyLocation();
		}
		return myLocationOverlay;
	}
}
